<?php

class Innovations_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = "mis_innovations";
        $this->load->model("functions_model");
    }

    function newInnovation($added_by, $file) {
        $data = $_POST;
        $data['file'] = $file;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->select("added_by,idea,beneficiaries,business_case,roi,status,feasilbility,remarks,date_added,imple_status,assess_remarks,file");
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['business_case']=nl2br($row['business_case']);
            $row['file'] = "<a href=" . base_url() . "uploads/" . $row['file'] . ">Download</a>";
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $result[] = $row;
        }
        return $result;
    }

    //    function getAll($username) {
    //      //  $data['added_by'] = $username;
    //        $this->db->select("tid,idea,beneficiaries,business_case,file,date_added");
    //   //     $this->db->where($data);
    //        $this->db->from($this->tableName);
    //        $this->db->order_by("tid", "desc");
    //        $result = array();
    //        $query = $this->db->get();
    //        foreach ($query->result_array() as $row) {
    //            $row['file'] = "<a href=" . base_url() . "uploads/" . $row['file'] . ">Download</a>";
    //            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
    //             $row['more']="<a href='javascript:loadUrl(\"#innov_result\",\"" . base_url() . "index.php?/more_info/innovationsMoreInfo/" . $row['tid'] . "\")'>More details</a>";
    //             unset($row['tid']);
    //            $result[] = $row;
    //        }
    //        return $result;
    //    }

    function adminGetAll($field=NULL, $search=NULL) {
        if ($field and $search) {
            if ($field == 'all') {
                $this->db->like("idea", $search);
                $this->db->or_like("business_case", $search);
                $this->db->or_like("beneficiaries", $search);
                $this->db->or_like("imple_status", $search);
                $this->db->or_like("assess_remarks", $search);
                $this->db->or_like("added_by", $search);
            } else {
                $data[$field] = $search;
                $this->db->like($data);
            }
        }
        $this->db->select("date_added, tid,idea,beneficiaries,business_case,file");
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $row['business_case']=nl2br($row['business_case']);
            //$row['added_by'] = "<a href='javascript:loadUrl(\"#innov_result\",\"" . base_url() . "index.php?/admin_mis/applicantProfile/$added_by\")'>" . $row['added_by'] . "</a>";
            $row['file'] = "<a href=" . base_url() . "uploads/" . $row['file'] . ">Download</a>";
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
            $row['more'] = "<a href='javascript:loadUrl(\"#innov_result\",\"" . base_url() . "index.php?/more_info/innovationsMoreInfo/" . $row['tid'] . "\")'><img src='" . base_url() . "/images/more.png' alt='More'/></a> ";

            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].="<a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteInnovation/$tid\",\"Are you sure to delete this record?\")'><img src='" . base_url() . "/images/delete.png' alt='Delete'/></a> ";
            }

            unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

}

?>
